home *** CD-ROM | disk | FTP | other *** search
/ 3D GFX / 3D GFX.iso / amiutils / m_p / povraywb / source.lzh / amiga.c next >
C/C++ Source or Header  |  1995-05-19  |  21KB  |  1,039 lines

  1. /*
  2. ** PovRay 2.2 Amiga Display Module for CyberGfx, ECS/AGA Chipset & Workbench 3.0 
  3. **
  4. ** 1995 by Frank Hoffmann 
  5. */
  6.  
  7.  
  8. #include "frame.h"
  9. #include "povproto.h"
  10.  
  11. #include <proto/exec.h>
  12. #include <proto/intuition.h>
  13. #include <proto/graphics.h>
  14. #include <proto/dos.h>
  15. #include <proto/expansion.h>
  16. #include <proto/utility.h>
  17. #include <exec/types.h>
  18. #include <intuition/intuition.h>
  19. #include <graphics/display.h>
  20. #include <graphics/displayinfo.h>
  21. #include <graphics/modeid.h>
  22. #include <graphics/gfxbase.h>
  23. #include <graphics/gfx.h>
  24. #include <graphics/videocontrol.h>
  25. #include <dos/dos.h>
  26.  
  27. #include <math.h>
  28. #ifdef _M68881
  29.  #include <m68881.h>
  30. #endif
  31.  
  32. //#define MATHERRHANDLER
  33.  
  34. extern long __oslibversion     = 39;
  35. extern struct GfxBase *GfxBase;
  36.  
  37. /* Externs from PovRay */
  38. extern char DisplayFormat;
  39. extern char PaletteOption;
  40. extern int  Stop_Flag;
  41.  
  42. /* Function Pointer for Pixel Routine */
  43. static void (*PixelRoutine)(short x,short y,UBYTE r,UBYTE g,UBYTE b);
  44.  
  45. /* General Info */
  46. static struct Screen   *screen;
  47.  
  48. /* ENV */
  49. static char    *PUBSCREENNAME;
  50.  
  51. /* START Window mode */
  52. static struct ColorMap *COLORMAP = NULL;
  53. static struct RastPort *RASTPORT = NULL;
  54. static struct Window   *RWindow = NULL;
  55.  
  56. static short XOFFSET = 0;
  57. static short YOFFSET = 0;
  58.  
  59. static ULONG ColorEntries[256];
  60.  
  61. static struct Task *WinTask;
  62. static int WinProc;
  63. static struct MsgPort *Window_Port;
  64. /* END Window mode */
  65.  
  66. /* Optimizations */
  67. static UBYTE PenLookUpHam8[4][4][4];
  68.  
  69. static struct colreg { ULONG r,g,b; };
  70. static struct colreg colback[64];
  71. static ULONG  regsused = 2;
  72.  
  73. /* Extern ScreenMode Module (modeid.c) */
  74. extern int GetScreenMode (ULONG *modeID, int *height, int *width  , int *autoscroll,int *depth,int,int,ULONG);
  75.  
  76. /*
  77. **
  78. ** Cybergraphics Part 
  79. **
  80. */
  81.  
  82. #include <pragmas/cybergraphics_pragmas.h>
  83. #include <libraries/cybergraphics.h>
  84.  
  85. /* CyberGfx Prototypes */
  86.  
  87. ULONG WriteRGBPixel(struct RastPort*,UWORD,UWORD,ULONG);
  88. BOOL IsCyberModeID(ULONG);
  89. ULONG GetCyberIDAttr(ULONG,ULONG);
  90.  
  91. struct Library *CyberGfxBase = NULL;
  92.  
  93. struct TagItem cybertags[] =
  94. {
  95.   {    CYBRMREQ_WinTitle ,(ULONG)"POVRay 24Bit-Render Screenselection" },
  96.   {    CYBRMREQ_MinWidth , 0 } ,
  97.   {    CYBRMREQ_MinHeight, 0 } ,
  98.   {    CYBRMREQ_MinDepth , 9 } , 
  99.   {    TAG_END, TAG_END }
  100. };
  101.  
  102.  
  103. struct TagItem cybermodetags[] =
  104. {
  105.  CYBRBIDTG_NominalWidth     ,0,
  106.  CYBRBIDTG_NominalHeight ,0,
  107.  CYBRBIDTG_Depth         ,24,
  108.  TAG_END, TAG_END
  109. };
  110.  
  111.  
  112. /*
  113. **
  114. ** Setup 
  115. **
  116. */
  117.  
  118. static void SetPOVTaskPri(LONG pri)
  119. {
  120.     struct Task* Iam;
  121.  
  122.     if(Iam = FindTask(NULL))
  123.     {
  124.         SetTaskPri(Iam,pri);
  125.     }
  126.     else
  127.     {
  128.         fprintf(stderr,"Error: failed to find myself.\n");
  129.     }    
  130. }
  131.  
  132. static void DoPOVTaskPri(void)
  133. {
  134.     char taskpribuf[20];
  135.  
  136.     if(GetVar("POVRAYTASKPRI",taskpribuf,sizeof(taskpribuf),NULL) != 0)
  137.     { 
  138.         SetPOVTaskPri(atol(taskpribuf));
  139.     }
  140. }
  141.  
  142. /*
  143. ** control functions
  144. */
  145.  
  146. int run_state = 0;
  147. int ChipSet;
  148.  
  149. #define CHIPSET_AGA 1
  150. #define CHIPSET_OLD 0
  151.  
  152. static int amiga_breakhandle(void)
  153. {
  154.     if(run_state)
  155.     {
  156.         amiga_close_all();
  157.         return (1);
  158.     }
  159.     else
  160.     {
  161.         return (0);
  162.     }
  163. }
  164.  
  165. void amiga_init_POVRAY(void )
  166. {
  167.    DoPOVTaskPri();
  168.    (void) onbreak(amiga_breakhandle);
  169.    if((GfxBase->ChipRevBits0 & GFXF_AA_ALICE) && (GfxBase->ChipRevBits0 & GFXF_AA_LISA))
  170.    {
  171.         ChipSet = CHIPSET_AGA;
  172.    }
  173.    else
  174.    {
  175.         ChipSet = CHIPSET_OLD;
  176.    }
  177.  
  178.    CyberGfxBase = OpenLibrary("cybergraphics.library",40);
  179. }
  180.  
  181. int amiga_close_all (void)
  182. {
  183.    fprintf(stderr,"\nBreak received!\n");
  184.    Stop_Flag = TRUE;
  185.    close_all();
  186.    return (1);
  187. }
  188.  
  189. #ifdef MATHERRHANDLER
  190. int __matherr (struct __exception *x)
  191. {
  192.    switch(x->type) 
  193.      {
  194.      case DOMAIN:
  195.      case OVERFLOW:
  196.         x->retval = 1.0e17;
  197.         break;
  198.  
  199.      case SING:
  200.      case UNDERFLOW:
  201.         x->retval = 0.0;
  202.         break;
  203.  
  204.      case TLOSS:
  205.      case PLOSS:
  206.         return (0);
  207.         break;
  208.       
  209.      case RANGE:
  210.         #ifdef MATHERRCOUNT
  211.         mathrange++;
  212.         #endif
  213.         return (0);
  214.         break;
  215.  
  216.      default:
  217.         return (0);
  218.         break;
  219.      }
  220.    return(1);
  221. }
  222. #endif
  223.  
  224. /*
  225. **
  226. ** Write Pixel Routines 
  227. **
  228. */
  229.  
  230. static void write_no_pixel(short x,short y,UBYTE r,UBYTE g,UBYTE b)
  231. /* dummy function to avoid system crash under certain error conditions */
  232. { }
  233.  
  234. static void write_windowsys_pixel(short x,short y,UBYTE r,UBYTE g,UBYTE b)
  235. /* system friendly color function */ 
  236. {
  237.     LONG color;
  238.  
  239.     if((color = ObtainBestPen(COLORMAP,r<<24,g<<24,b<<24,OBP_Precision,PRECISION_IMAGE,TAG_DONE,TAG_DONE))==-1)
  240.     {
  241.         fprintf(stderr,"\nCould not find color!");
  242.         color = 0;
  243.     }
  244.     SetAPen(RASTPORT,(UBYTE)color);
  245.     WritePixel(RASTPORT,XOFFSET+x,YOFFSET+y);
  246.     ColorEntries[color]++;
  247. }
  248.  
  249. static void write_windowgrey_pixel(short x, short y,UBYTE r,UBYTE g,UBYTE b)
  250. /* system friendly gray-color function */
  251. {
  252.     LONG color;
  253.     ULONG i = (ULONG)(0.3*(double)(b<<24)+0.59*(double)(g<<24)+ 0.11*(double)(b<<24));
  254.  
  255.     if((color = ObtainBestPen(COLORMAP,i,i,i,OBP_Precision,PRECISION_IMAGE,TAG_DONE,TAG_DONE))==-1)
  256.     {
  257.         fprintf(stderr,"\nCould not find color!");
  258.         color = 0;
  259.     }
  260.     SetAPen(RASTPORT,(UBYTE)color);
  261.     WritePixel(RASTPORT,XOFFSET+x,YOFFSET+y);
  262.     ColorEntries[color]++;
  263. }
  264.  
  265. static void write_gray8_pixel(short x, short y,UBYTE r,UBYTE g,UBYTE b)
  266. /* system friendly gray-color function with fixed palette */
  267. {
  268.     SetAPen(RASTPORT,(UBYTE)(0.3*(double)b+0.59*(double)g+ 0.11*(double)b));
  269.     WritePixel(RASTPORT,XOFFSET+x,YOFFSET+y);
  270. }
  271.  
  272. #define absdif(x,y) ((x > y) ? (x - y) : (y - x))
  273. #define max3(x,y,z) ((x>y)?((x>z)?1:3):((y>z)?2:3))
  274.  
  275. static short lasty;
  276. static UBYTE rl,gl,bl;
  277.  
  278. #define T_MASK 0xC0
  279. #define MOD_BLUE    64
  280. #define MOD_RED        128
  281. #define MOD_GREEN    192
  282.  
  283. static short last_x = 0,last_y = 0;
  284. static UBYTE last_red = 0, last_green = 0,last_blue = 0;
  285.  
  286. __inline static WORD close(UBYTE r,UBYTE g,UBYTE b)
  287. {
  288.     return (WORD)( ((r&0xc0)>>2) + ((g&0xc0)>>4) + ((b&0xc0)>>6));
  289. }
  290.  
  291. __inline static WORD AllocRec(UBYTE r,UBYTE g,UBYTE b)
  292. {
  293.     return close(r,g,b);
  294. }
  295.  
  296. static void write_ham8_pixel(short x,short y,UBYTE r,UBYTE g,UBYTE b)
  297. {
  298.    UBYTE Red,Green,Blue,delta_red,delta_green,delta_blue;
  299.    UBYTE color;
  300.    BOOL  RegUsed = FALSE;
  301.    WORD  colorreg;
  302.  
  303.    Red         = (r >> 2) & 0x3F;
  304.    Green     = (g >> 2) & 0x3F;
  305.    Blue     = (b >> 2) & 0x3F;
  306.  
  307.    if(x!=0)
  308.    {
  309.        if(last_x +1 != x || last_y != y)
  310.            return;
  311.    }
  312.  
  313.    if (last_y != y) 
  314.    {
  315.       last_y = y;
  316.  
  317.       /* Try to select register !? *) */
  318.  
  319.           if((colorreg = AllocRec(r,g,b)) >= 0)
  320.           {
  321.             last_red     = colback[colorreg].r>>24;
  322.               last_green     = colback[colorreg].g>>24;
  323.               last_blue     = colback[colorreg].b>>24;
  324.             RegUsed     = TRUE;
  325.             color       = (UBYTE) colorreg;
  326.           }
  327.           else
  328.           {
  329.               last_red     = colback[0].r>>24;
  330.               last_green     = colback[0].g>>24;
  331.               last_blue     = colback[0].b>>24;
  332.         }
  333.    }
  334.  
  335.    last_x = x;
  336.     
  337.    if(!RegUsed)
  338.    {
  339.            delta_red     = absdif (r,         last_red);
  340.            delta_green = absdif (g,         last_green);
  341.            delta_blue     = absdif (b,         last_blue);
  342.  
  343.            /* if error in more than one channel greater than x, 
  344.               try to select register with accuracy y
  345.               if fail, use old method *)
  346.            */ 
  347.         if((delta_red*delta_red + delta_green*delta_green + delta_blue*delta_blue) > (63*63*3))
  348.         {
  349.             if((colorreg = AllocRec(r,g,b)) >= 0)
  350.               {
  351.                 last_red     = colback[colorreg].r>>24;
  352.                   last_green     = colback[colorreg].g>>24;
  353.                   last_blue     = colback[colorreg].b>>24;
  354.                 RegUsed     = TRUE;
  355.                 color       = (UBYTE) colorreg;
  356.               }
  357.               else
  358.               {
  359.                   last_red     = colback[0].r>>24;
  360.                   last_green     = colback[0].g>>24;
  361.                   last_blue     = colback[0].b>>24;
  362.             }    
  363.         }
  364.  
  365.         if(!RegUsed)
  366.         {
  367.                switch (max3(delta_red, delta_green, delta_blue)) 
  368.                {
  369.                   case 1:
  370.                      last_red = r;
  371.                      color = MOD_RED     + Red;
  372.                      break;
  373.                   case 2:
  374.                      last_green = g;
  375.                      color = MOD_GREEN     + Green;
  376.                      break;
  377.                   case 3:
  378.                      last_blue = b;
  379.                      color = MOD_BLUE     + Blue;
  380.                      break;
  381.             }
  382.            }        
  383.    }
  384.  
  385.    SetAPen(RASTPORT, color);
  386.    WritePixel(RASTPORT, XOFFSET+x, YOFFSET+y);
  387. }
  388.  
  389. static void write_ham6_pixel(short x,short y,UBYTE r,UBYTE g,UBYTE b)
  390. /*
  391. ** Routine from Povray 2.2 orginal Sourcecode 
  392. */
  393. {
  394.    UBYTE Red,Green,Blue,delta_red,delta_green,delta_blue;
  395.    UBYTE color;
  396.  
  397.    Red         = (r >> 4) & 0x0F;
  398.    Green     = (g >> 4) & 0x0F;
  399.    Blue     = (b >> 4) & 0x0F;
  400.  
  401.    if(x!=0)
  402.    {
  403.        if(last_x +1 != x || last_y != y)
  404.            return;
  405.    }
  406.  
  407.    if (last_y != y) 
  408.    {
  409.       last_y = y;
  410.       last_red = last_green = last_blue = 0;
  411.    }
  412.  
  413.    last_x = x;
  414.  
  415.    delta_red     = absdif (Red,         last_red);
  416.    delta_green     = absdif (Green,     last_green);
  417.    delta_blue     = absdif (Blue,     last_blue);
  418.  
  419.    switch (max3(delta_red, delta_green, delta_blue)) 
  420.    {
  421.       case 1:
  422.          last_red = Red;
  423.          color = 0x20 + Red;
  424.          break;
  425.       case 2:
  426.          last_green = Green;
  427.          color = 0x30 + Green;
  428.          break;
  429.       case 3:
  430.          last_blue = Blue;
  431.          color = 0x10 + Blue;
  432.          break;
  433.    }
  434.  
  435.    SetAPen(RASTPORT, color);
  436.    WritePixel(RASTPORT, XOFFSET+x, YOFFSET+y);
  437. }
  438.  
  439. static void write_Cyber24_pixel(short x, short y,UBYTE r,UBYTE g,UBYTE b)
  440. /* CyberGfx direct Color pixel routine */
  441. {
  442.     ULONG color = (((ULONG)r)<<16) + (((ULONG)g)<<8) + ((ULONG)b);
  443.     /* Note: Alpha Channel remains zero */
  444.  
  445.         WriteRGBPixel(RASTPORT,XOFFSET+x,YOFFSET+y,color);
  446. }
  447.  
  448. /*
  449. **
  450. ** Init and Clean Routines
  451. **
  452. */
  453.  
  454. static void DoCube4(struct Screen* screen)
  455. /* Generate 4*4*4 = 64 Palette for HAM 8 Picture */
  456. {
  457.     ULONG r,g,b;
  458.     UBYTE c=0;
  459.  
  460.     struct cmap
  461.     {    
  462.         ULONG col;
  463.         ULONG c[64][3];
  464.         ULONG pad;
  465.     }cmap;
  466.  
  467.     cmap.col = (64l<<16)+0;
  468.     cmap.pad = 0;
  469.  
  470.     for(r=0;r<4;r++)
  471.     {
  472.         for(g=0;g<4;g++)
  473.         {
  474.             for(b=0;b<4;b++)
  475.             {
  476.                 cmap.c[r*4*4+g*4+b][0] = (0xffffffff/3)*r;
  477.                 cmap.c[r*4*4+g*4+b][1] = (0xffffffff/3)*g;
  478.                 cmap.c[r*4*4+g*4+b][2] = (0xffffffff/3)*b;
  479.                 PenLookUpHam8[r][g][b] = c;
  480.                 c++;
  481.             }
  482.         }
  483.     }        
  484.  
  485.     LoadRGB32(&screen->ViewPort,(ULONG*)&cmap);
  486. }
  487.  
  488. void display_finished (void)
  489. {
  490.    if(RWindow)
  491.    { 
  492.       fprintf(stderr,"\nFinished.\nClose window to quit\n");
  493.    }
  494. }
  495.  
  496. void display_init (int input_width,int input_height)
  497. {
  498.    ULONG i;
  499.    char namebuffer[256];
  500.  
  501.    PixelRoutine  = write_no_pixel;
  502.    PUBSCREENNAME = NULL;
  503.    screen        = NULL;    
  504.    RWindow       = NULL;
  505.  
  506.    run_state = 1; // enable CTRL-C
  507.  
  508.    memset(ColorEntries,0,256);
  509.  
  510.    if(DisplayFormat == 'W')
  511.    {
  512.         ULONG modeid;
  513.  
  514.         /* if POVWINDOWSCREEN is defined, copy it to PUBSCEENNAME */
  515.         if(GetVar("POVWINDOWSCREEN",namebuffer,256,NULL)!=-1)
  516.         {
  517.             PUBSCREENNAME = namebuffer;
  518.         }
  519.  
  520.         if(!(screen = LockPubScreen(PUBSCREENNAME)))
  521.         {
  522.             fprintf(stderr,"\nCould not lock public screen!\n");
  523.             return;
  524.         }
  525.         
  526.         XOFFSET = screen->WBorLeft;
  527.         YOFFSET = screen->WBorTop+screen->Font->ta_YSize+1;
  528.  
  529.         if(((XOFFSET+screen->WBorRight+input_width) > screen->Width)
  530.             ||((YOFFSET+screen->WBorBottom+input_height) > screen->Height))
  531.         {
  532.             fprintf(stderr,"\nCould not open window of inner size: with %d height %d.\n",input_width,input_height);  
  533.             UnlockPubScreen(NULL,screen);
  534.             return;
  535.         }
  536.  
  537.         if(!(RWindow = OpenWindowTags(NULL,
  538.             WA_PubScreen,        screen,
  539.             WA_Left,            screen->MouseX,
  540.             WA_Top,                screen->MouseY,
  541.             WA_InnerWidth,        input_width,
  542.             WA_InnerHeight,        input_height,
  543.             WA_IDCMP,            IDCMP_ACTIVEWINDOW|IDCMP_INACTIVEWINDOW|
  544.                                 IDCMP_CLOSEWINDOW,
  545.             WA_DragBar,            TRUE,
  546.             WA_DepthGadget,     TRUE,
  547.             WA_RMBTrap,            TRUE,
  548.             WA_CloseGadget,     TRUE,
  549.             WA_ScreenTitle,     "POV Ray Rendering...",
  550.             WA_Title,            "POV Ray Window",
  551.             WA_GimmeZeroZero,    TRUE,
  552.             TAG_END,TAG_END)))
  553.         {
  554.             fprintf(stderr,"\nCould not open Window\n");
  555.             UnlockPubScreen(NULL,screen);
  556.             return;
  557.         }
  558.  
  559.         XOFFSET=0;
  560.         YOFFSET=0;
  561.  
  562.         /* set needed structs */
  563.         RASTPORT = RWindow->RPort;
  564.         COLORMAP = screen->ViewPort.ColorMap;
  565.  
  566.         modeid = GetVPModeID(&screen->ViewPort);
  567.  
  568.         UnlockPubScreen(NULL,screen);
  569.  
  570.         if(PaletteOption == 'G')
  571.         {
  572.             PixelRoutine = write_windowgrey_pixel;
  573.         }
  574.         else if(PaletteOption == 'S')
  575.         {
  576.             PixelRoutine = write_windowsys_pixel;
  577.         }
  578.         else
  579.         {
  580.             PixelRoutine = write_windowsys_pixel;
  581.  
  582.             if(CyberGfxBase)
  583.             {
  584.                 if(IsCyberModeID(modeid))
  585.                 {
  586.                     if(GetCyberIDAttr(CYBRIDATTR_PIXFMT,modeid) != PIXFMT_LUT8)
  587.                     {
  588.                         PixelRoutine = write_Cyber24_pixel;
  589.                     }
  590.                 }
  591.             }                
  592.         }
  593.  
  594.         return;
  595.    }
  596.    else if( (DisplayFormat == 'P') || (DisplayFormat == 'S'))
  597.    {
  598.         ULONG modeid, inID;
  599.         struct DimensionInfo dinfo;
  600.         char *dummy;
  601.         int as,sheight,swidth,depth = 8;
  602.  
  603.         modeid = BestModeID(BIDTAG_NominalWidth        ,input_width,
  604.                             BIDTAG_NominalHeight    ,input_height,
  605.                             BIDTAG_Depth            , 8,
  606.                             BIDTAG_RedBits            ,(UBYTE)8,
  607.                             TAG_END);
  608.         inID = modeid;        
  609.  
  610.         if(modeid == INVALID_ID)
  611.         {
  612.             /* DO ECS STUFF ? */
  613.             fprintf(stderr,"\nNo matching modeid found!\n");
  614.             return;
  615.         }
  616.  
  617.         if(PaletteOption == '1')
  618.         {
  619.             if(GetVar("POVSCREENMODEID",namebuffer,256,NULL)!=-1)
  620.             {
  621.  
  622.                 inID = strtol(namebuffer,&dummy,16);
  623.                 if(inID != 0) modeid = inID;
  624.                 if(ModeNotAvailable(modeid) != NULL)
  625.                 {
  626.                     return;
  627.                 }
  628.             }
  629.         }
  630.  
  631.         if(PaletteOption == '2')
  632.         {
  633.                 modeid = BestModeID(
  634.                             BIDTAG_NominalWidth        ,input_width,
  635.                             BIDTAG_NominalHeight    ,input_height,
  636.                             BIDTAG_DesiredWidth        ,input_width,
  637.                             BIDTAG_DesiredHeight    ,input_height,
  638.                             BIDTAG_Depth            , 8,
  639.                             BIDTAG_RedBits            ,(UBYTE)8,
  640.                             TAG_END);
  641.         }
  642.  
  643.         if(!GetDisplayInfoData(NULL,(UBYTE*)&dinfo,sizeof(struct DimensionInfo),DTAG_DIMS,modeid))
  644.         {
  645.             fprintf(stderr,"Could not get dimensions information form screenmode!\n");
  646.             exit(0);
  647.         }
  648.  
  649.         if(PaletteOption == '0')
  650.         {
  651.             depth = 1;
  652.              if(GetScreenMode(&inID,&sheight,&swidth,&as,&depth,input_width,input_height,DIPF_IS_WB))
  653.             {
  654.                 modeid = inID;
  655.             }
  656.             else
  657.             {
  658.                 depth   = 8;
  659.                 swidth  = dinfo.Nominal.MaxX;
  660.                 sheight = dinfo.Nominal.MaxY;
  661.             }
  662.         }
  663.         else if(PaletteOption == 'G')
  664.         {
  665.             depth = -8;
  666.             if(GetScreenMode(&inID,&sheight,&swidth,&as,&depth,input_width,input_height,DIPF_IS_WB))
  667.             {
  668.                 modeid = inID;
  669.             }
  670.             else
  671.             {
  672.                 depth   = 8;
  673.                 swidth  = dinfo.Nominal.MaxX;
  674.                 sheight = dinfo.Nominal.MaxY;
  675.             }        
  676.         }
  677.         else
  678.         {
  679.             swidth = dinfo.Nominal.MaxX;
  680.             sheight = dinfo.Nominal.MaxY;
  681.         }
  682.         
  683.  
  684.         XOFFSET = (swidth  - input_width)/2;
  685.         YOFFSET = (sheight - input_height)/2;
  686.  
  687.         if(DisplayFormat == 'P')
  688.         {
  689.  
  690.             if(!(screen = OpenScreenTags(NULL,
  691.                 SA_Width,        swidth, 
  692.                 SA_Height,        sheight, 
  693.                 SA_Depth,        depth,
  694.                 SA_Type,        PUBLICSCREEN,
  695.                 SA_PubName,        PUBSCREENNAME,
  696.                 SA_DisplayID,    modeid,
  697.                 SA_Behind,      TRUE,
  698.                 SA_SharePens,    TRUE,
  699.                 SA_Title,        "POVRay Rendering Screen",
  700.                 TAG_END)))
  701.             {
  702.                 fprintf(stderr,"\nCould not open screen!\n");
  703.                 return;
  704.             }
  705.  
  706.             if(!(RWindow = OpenWindowTags(NULL,
  707.                 WA_PubScreen,        screen,
  708.                 WA_Left,            XOFFSET,
  709.                 WA_Top,                YOFFSET,
  710.                 WA_InnerWidth,        input_width,
  711.                 WA_InnerHeight,        input_height,
  712.                 WA_IDCMP,            IDCMP_CLOSEWINDOW|IDCMP_RAWKEY,
  713.                 WA_DragBar,            TRUE,
  714.                 WA_DepthGadget,     TRUE,
  715.                 WA_RMBTrap,            TRUE,
  716.                 WA_CloseGadget,     TRUE,
  717.                 WA_GimmeZeroZero,    TRUE,
  718.                 TAG_END,TAG_END)))
  719.             {
  720.                 fprintf(stderr,"\nCould not open Window\n");
  721.                 CloseScreen(screen);
  722.                 return;
  723.             }
  724.             XOFFSET = 0;
  725.             YOFFSET = 0; 
  726.             RASTPORT = RWindow->RPort;
  727.         }
  728.         else
  729.         {    
  730.             if(!(screen = OpenScreenTags(NULL,
  731.                 SA_Width,        swidth, 
  732.                 SA_Height,        sheight, 
  733.                 SA_Depth,        depth,
  734.                 SA_Type,        CUSTOMSCREEN,
  735.                 SA_PubName,        PUBSCREENNAME,
  736.                 SA_DisplayID,    modeid,
  737.                 SA_SharePens,    TRUE,
  738.                 SA_Behind,      TRUE,
  739.                 SA_Quiet,       TRUE,
  740.                 SA_Title,        "POVRay Rendering Screen",
  741.                 TAG_END)))
  742.             {
  743.                 fprintf(stderr,"\nCould not open screen!\n");
  744.                 return;
  745.             }
  746.  
  747.             /* to get intuition messages for now */
  748.             if(!(RWindow = OpenWindowTags(NULL,
  749.                 WA_CustomScreen,    screen,
  750.                 WA_Left,            0,
  751.                 WA_Top,                0,
  752.                 WA_InnerWidth,        swidth,
  753.                 WA_InnerHeight,        sheight,
  754.                 WA_IDCMP,            IDCMP_CLOSEWINDOW|IDCMP_RAWKEY,
  755.                 WA_DragBar,            FALSE,
  756.                 WA_DepthGadget,     FALSE,
  757.                 WA_RMBTrap,            TRUE,
  758.                 WA_CloseGadget,     FALSE,
  759.                 WA_GimmeZeroZero,    TRUE,
  760.                 WA_Borderless,        TRUE,
  761.                 WA_Backdrop,        TRUE,                
  762.                 TAG_END,TAG_END)))
  763.             {
  764.                 fprintf(stderr,"\nCould not open Window\n");
  765.                 CloseScreen(screen);
  766.                 return;
  767.             } 
  768.             RASTPORT = &screen->RastPort;
  769.         }
  770.         
  771.         COLORMAP = screen->ViewPort.ColorMap;
  772.  
  773.         if(PaletteOption == 'G')
  774.         {
  775.             for(i=0;i<256;i++)
  776.             {
  777.                 SetRGB32(&screen->ViewPort,i,i<<24,i<<24,i<<24);
  778.             }    
  779.             PixelRoutine = write_gray8_pixel;
  780.         }
  781.         else if(PaletteOption == 'F')
  782.         {
  783.             PixelRoutine = write_windowgrey_pixel;
  784.         }
  785.         else
  786.         {
  787.  
  788.             PixelRoutine = write_windowsys_pixel;    
  789.  
  790.             if(CyberGfxBase)
  791.             {
  792.                 if(IsCyberModeID(modeid))
  793.                 {
  794.                     if(GetCyberIDAttr(CYBRIDATTR_PIXFMT,modeid) != PIXFMT_LUT8)
  795.                     {
  796.                         PixelRoutine = write_Cyber24_pixel;
  797.                     }
  798.                 }
  799.             }
  800.         }
  801.  
  802.         if(screen) ScreenToFront(screen);
  803.    }
  804.    else if(DisplayFormat == 'H')
  805.    {
  806.         ULONG modeid, inID;
  807.         struct DimensionInfo dinfo;
  808.         char *dummy;
  809.         int sheight,swidth,depth = 8,as;
  810.         
  811.         lasty = -1;
  812.         rl = 0; gl = 0; bl = 0;
  813.  
  814.         modeid = BestModeID(BIDTAG_NominalWidth        ,input_width,
  815.                             BIDTAG_NominalHeight    ,input_height,
  816.                             BIDTAG_Depth            , 8,
  817.                             BIDTAG_RedBits            ,(UBYTE)8,
  818.                             BIDTAG_DIPFMustHave     , DIPF_IS_HAM,
  819.                             TAG_END);
  820.         inID = modeid;
  821.  
  822.         if(modeid == INVALID_ID)
  823.         {
  824.             /* DO ECS STUFF ? */
  825.             fprintf(stderr,"\nNo matching modeid found!\n");
  826.             return;
  827.         }
  828.  
  829.         if(PaletteOption == '2' || PaletteOption == '3')
  830.         {
  831.             if(GetVar("POVRAYHAMMODEID",namebuffer,256,NULL)!=-1)
  832.             {
  833.  
  834.                 inID = strtol(namebuffer,&dummy,16);
  835.                 if(inID != 0) modeid = inID;
  836.                 if(ModeNotAvailable(modeid) != NULL)
  837.                 {
  838.                     return;
  839.                 } 
  840.             }
  841.         }
  842.  
  843.         if(PaletteOption == '1')
  844.         {
  845.             
  846.             if(ChipSet = CHIPSET_AGA) depth = 8; else depth = 6;
  847.  
  848.             modeid = BestModeID(
  849.                             BIDTAG_NominalWidth        ,input_width,
  850.                             BIDTAG_NominalHeight    ,input_height,
  851.                             BIDTAG_DesiredWidth        ,input_width,
  852.                             BIDTAG_DesiredHeight    ,input_height,
  853.                             BIDTAG_Depth            ,depth,
  854.                             BIDTAG_DIPFMustHave     ,DIPF_IS_HAM,
  855.                             TAG_END);
  856.         }
  857.  
  858.         if(GetDisplayInfoData(NULL,(UBYTE*)&dinfo,sizeof(struct DimensionInfo),DTAG_DIMS,modeid))
  859.         {
  860.             
  861.         }
  862.  
  863.         if(PaletteOption == '0')
  864.         {
  865.             depth = 6;
  866.              if(GetScreenMode(&inID,&sheight,&swidth,&as,&depth,input_width,input_height,DIPF_IS_HAM))
  867.             {
  868.                 modeid = inID;
  869.             }
  870.             else
  871.             {
  872.                 depth = 8;
  873.                 swidth = dinfo.Nominal.MaxX;
  874.                 sheight = dinfo.Nominal.MaxY;
  875.             }
  876.         }
  877.         else
  878.         {
  879.             swidth = dinfo.Nominal.MaxX;
  880.             sheight = dinfo.Nominal.MaxY;
  881.         }
  882.  
  883.         XOFFSET = (swidth  - input_width)/2;
  884.         YOFFSET = (sheight - input_height)/2;
  885.  
  886.         if(PaletteOption == '2')
  887.         {
  888.             if(ChipSet == CHIPSET_AGA)
  889.             {
  890.                 depth  = 8;    
  891.             }
  892.             else
  893.             {
  894.                 depth  = 6;
  895.             }
  896.         }
  897.         
  898.         if(PaletteOption == '3')
  899.         {
  900.             depth = 6;
  901.         }
  902.  
  903.         if(!(screen = OpenScreenTags(NULL,    
  904.                 SA_Top,         0,
  905.                 SA_Left,        0,
  906.                 SA_Width,        swidth, 
  907.                 SA_Height,        sheight, 
  908.                 SA_Depth,        depth,
  909.                 SA_Type,        CUSTOMSCREEN,
  910.                 SA_DisplayID,    modeid,
  911.                 SA_Quiet,       TRUE,
  912.                 SA_AutoScroll,  TRUE,
  913.                 SA_Draggable,   TRUE,
  914.                 SA_Behind,      TRUE,
  915.                 SA_Interleaved, TRUE,        
  916.                 SA_Title,        NULL,
  917.                 TAG_END)))
  918.             {
  919.                 fprintf(stderr,"\nCould not open screen!\n");
  920.                 return;
  921.             }
  922.  
  923.             /* to get intuition messages */
  924.             if(!(RWindow = OpenWindowTags(NULL,
  925.                 WA_CustomScreen,    screen,
  926.                 WA_Left,            0,
  927.                 WA_Top,                0,
  928.                 WA_InnerWidth,        swidth,   
  929.                 WA_InnerHeight,        sheight,
  930.                 WA_IDCMP,            IDCMP_CLOSEWINDOW|IDCMP_RAWKEY,
  931.                 WA_DragBar,            FALSE,
  932.                 WA_DepthGadget,     FALSE,
  933.                 WA_RMBTrap,            TRUE,
  934.                 WA_CloseGadget,     FALSE,
  935.                 WA_GimmeZeroZero,    TRUE,
  936.                 WA_Borderless,        TRUE,
  937.                 WA_Backdrop,        TRUE,
  938.                 WA_Activate,        TRUE,        
  939.                 TAG_END,TAG_END)))
  940.             {
  941.                 fprintf(stderr,"\nCould not open Window\n");
  942.                 CloseScreen(screen);
  943.                 return;
  944.             } 
  945.  
  946.            RASTPORT = &screen->RastPort;
  947.         COLORMAP = screen->ViewPort.ColorMap;
  948.  
  949.         DoCube4(screen);
  950.  
  951.         last_y = -1;
  952.         last_x = -1;
  953.  
  954.         if(((PaletteOption == '2') || (PaletteOption == '0')) && (depth == 8) && (ChipSet == CHIPSET_AGA))
  955.         {
  956.             GetRGB32(screen->ViewPort.ColorMap,0,64,(ULONG*)colback);
  957.  
  958.             PixelRoutine = write_ham8_pixel; // write_ham8_pixel;
  959.         }
  960.         else
  961.         {
  962.             PixelRoutine = write_ham6_pixel;
  963.         }
  964.  
  965.         if(screen) ScreenToFront(screen);
  966.     }
  967. }
  968.  
  969. void display_close (void)
  970. {
  971.     if(RWindow)
  972.     {
  973.         ULONG i,j;
  974.         ULONG colors = 0;
  975.         struct IntuiMessage* IMes;
  976.         ULONG class;
  977.         BOOL running = TRUE;
  978.         
  979.         if(Stop_Flag != TRUE)
  980.         {
  981.             ULONG Signal;
  982.  
  983.             DisplayBeep(NULL);
  984.  
  985.             run_state = 0;
  986.             do
  987.             {
  988.                 // WaitPort(RWindow->UserPort);
  989.                 Signal = Wait(SIGBREAKF_CTRL_C | (1<< RWindow->UserPort->mp_SigBit));
  990.                 
  991.                 if(Signal & (1<< RWindow->UserPort->mp_SigBit))
  992.                 {
  993.                     IMes = (struct IntuiMessage*) GetMsg(RWindow->UserPort);
  994.             
  995.                     class = IMes->Class;
  996.                     ReplyMsg((struct Message*)IMes);
  997.  
  998.                     if(class == IDCMP_CLOSEWINDOW) running = FALSE;
  999.                     if(class == IDCMP_RAWKEY) running = FALSE;
  1000.                 }
  1001.                 else if (Signal & SIGBREAKF_CTRL_C)
  1002.                 {
  1003.                     running = FALSE;
  1004.                 }        
  1005.  
  1006.             }while(running);
  1007.         }
  1008.  
  1009.         CloseWindow(RWindow);
  1010.     
  1011.         for(i=0;i<256;i++) 
  1012.         {
  1013.             if(ColorEntries[i] > 0) 
  1014.             {
  1015.                 colors++;
  1016.  
  1017.                 for(j=0;j<ColorEntries[i];j++)
  1018.                 {
  1019.                     ReleasePen(COLORMAP,i);
  1020.                 }
  1021.             }
  1022.         }
  1023.         if(colors) fprintf(stderr,"\n%lu colors used!\n",colors);
  1024.     }
  1025.  
  1026.     if(screen) 
  1027.     {
  1028.         if(DisplayFormat == 'W') UnlockPubScreen(NULL,screen);
  1029.         else                     CloseScreen(screen);
  1030.     }
  1031.  
  1032.     if(CyberGfxBase) { CloseLibrary(CyberGfxBase); CyberGfxBase = NULL; } 
  1033. }
  1034.  
  1035. void display_plot (int x,int y,UBYTE Red,UBYTE Green,UBYTE Blue)
  1036. {
  1037.     PixelRoutine((short)x,(short)y,Red,Green,Blue);
  1038. }
  1039.